package com.unsafe;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 解决Set不安全的问题
 */
public class SetTest {
    public static void main(String[] args) {
        /**
         *1、 Set<Integer> set = Collections.synchronizedSet(new HashSet<>());
         * 2、Set<Integer> set = new CopyOnWriteArraySet<Integer>();
         */
        Set<Integer> set = new CopyOnWriteArraySet<Integer>();

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            new Thread(() -> {
                set.add(finalI);
                System.out.println(set);
            }, "i").start();
        }
        System.out.println("最后的" + set.size());
    }
}
